import * as bajaService from '@/services/bajaService';
import * as historyDataService from '@/services/historyData';
import * as alarmDataService from '@/services/alarmData';

export default {
  namespace: 'dashboard',
  state: {
    layouts: '{}',
    layoutLock: true,
    charts: []
  },
  reducers: {
    setData(state, { payload }) {
      return { ...state, ...payload };
    },
  },
  effects: {
    *fetch({ payload: {dashboardRootOrd} }, { call, put, select }) {
      let charts = [], layouts = '{}', layoutLock = true;
      const dashboardRoot = yield call(bajaService.Ord, dashboardRootOrd);
      if(dashboardRoot) {
        const navOrd = dashboardRoot.getNavOrd().toString();
        layouts = dashboardRoot.getLayouts()===''?yield select(state => state.dashboard.layouts):dashboardRoot.getLayouts();
        layoutLock = dashboardRoot.getLayoutLock();
        const chartGroup = yield call(bajaService.Ord, navOrd + '/chartGroup');
        if(chartGroup) {
          charts = yield call(bajaService.fetchNavChildren, chartGroup);
          if(charts) {
            for(let i=0; i<charts.length; i++) {
              const items = yield call(bajaService.fetchNavChildren, charts[i]);
              if(items) {
                let h = '', a = '';
                items.forEach((item, index) => {
                  // item.fontSize = typeof item.getFontSize !== 'undefined' ? item.getFontSize() : '16px';
                  // item.getFontSize = function() {
                  //   return item.fontSize;
                  // }
                  item.NOrd = typeof item.getOrd !== 'undefined' ? item.getOrd().$ord : 'unused';
                  item.getNOrd = function() {
                    return item.NOrd;
                  }
                  item.NHistoryOrd = typeof item.getHistoryOrd !== 'undefined' ? item.getHistoryOrd().$ord : 'null';
                  item.getNHistoryOrd = function() {
                    return this.NHistoryOrd;
                  }
                  if (item.NHistoryOrd !== 'null') {
                    h += item.getTitle() + '||' + item.NHistoryOrd;
                    if (index !== items.length - 1) {
                      h += '|||';
                    }
                  }

                  item.NAlarmClassOrd = typeof item.getAlarmClassOrd !== 'undefined' ? item.getAlarmClassOrd().$ord : 'null';
                  item.getNAlarmClassOrd = function() {
                    return this.NAlarmClassOrd;
                  }
                  if (item.NAlarmClassOrd !== 'null') {
                    a += item.getTitle() + '||' + item.NAlarmClassOrd;
                    if (index !== items.length - 1) {
                      a += '|||';
                    }
                  }
                });
                if (h !== '') {
                  const response = yield call(historyDataService.fetch, { h, pageNo: -1, pageSize: 5 });
                  if (response && typeof response.state !== undefined && response.state === 0) {
                    let name = [], list = [];
                    response.data.datas.forEach((data) => {
                      name.push(data.name);
                      list.push(data.list);
                    });
                    charts[i].historyName = name;
                    charts[i].historyList = list;
                  }
                }
                if (a !== '') {
                  const response = yield call(alarmDataService.getRealAlarmData, { h:a, pageNo: -1, pageSize: 5 });
                  if (response && typeof response.state !== undefined && response.state === 0) {
                    charts[i].alarmList = response.data.list;
                  }
                }
              }
              charts[i].NNavChildren = items;
              charts[i].getNNavChildren = function() {
                return this.NNavChildren;
              }
              // charts[i].fontSize = typeof charts[i].getFontSize !== 'undefined' ? charts[i].getFontSize() : '16px';
              // charts[i].getFontSize = function() {
              //   return charts[i].fontSize;
              // }
              charts[i].NOrd = typeof charts[i].getOrd !== 'undefined'?charts[i].getOrd().$ord:'null';
              charts[i].getNOrd = function() {
                return this.NOrd;
              }
              charts[i].NImage = typeof charts[i].getImage !== 'undefined'?charts[i].getImage().$ord:'null';
              charts[i].getNImage = function() {
                return this.NImage;
              }
              charts[i].NTextIcon = typeof charts[i].getTextIcon !== 'undefined'?charts[i].getTextIcon().$ord:'null';
              charts[i].getNTextIcon = function() {
                return this.NTextIcon;
              }

            }
          } else {
            charts = [];
          }
        }
      }
      yield put({ type: 'setData', payload: {charts, layouts, layoutLock}});
    },
    *clearAll({ _ }, { put }) {
      yield put({ type: 'setData', payload: {charts: [], layouts: '{}', layoutLock: true}});
    },
    *setLayouts({ payload: {dashboardRootOrd, layouts} }, { call, put }) {
      const dashboardRoot = yield call(bajaService.Ord, dashboardRootOrd);
      if(dashboardRoot) {
        layouts = JSON.stringify({'layouts':layouts});
        dashboardRoot.setLayouts(layouts);
        yield put({ type: 'setData', payload: {layouts}});
      }
    }
  },
  // subscriptions: {
  //   setup({ dispatch, history }) {
  //     return history.listen(({ pathname }) => {
  //       console.log("dashboard subscriptions()", pathname);
  //       if (pathname === '/') {
  //         dispatch({ type: 'fetch'});
  //       }
  //     });
  //   },
  // },
}
